package com.ruoyi.house.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.user.domain.TbOwner;
import com.ruoyi.user.service.ITbOwnerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.house.domain.TbHouse;
import com.ruoyi.house.service.ITbHouseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.house.domain.vo.TbHouseVo;
import com.ruoyi.user.service.ITbOwnerService;

/**
 * 房屋管理Controller
 * 
 * @author ruoyi
 * @date 2024-10-08
 */
@RestController
@RequestMapping("/house/house")
public class TbHouseController extends BaseController
{
    @Autowired
    private ITbHouseService tbHouseService;

    @Autowired
    private ITbOwnerService tbOwnerService;

    /**
     * 查询房屋管理列表
     */
    @PreAuthorize("@ss.hasPermi('house:house:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbHouse tbHouse)
    {
        startPage();
        List<TbHouseVo> list = tbHouseService.selectTbHouseVoList(tbHouse);
        return getDataTable(list);
    }

    /**
     * 导出房屋管理列表
     */
    @PreAuthorize("@ss.hasPermi('house:house:export')")
    @Log(title = "房屋管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbHouse tbHouse)
    {
        List<TbHouse> list = tbHouseService.selectTbHouseList(tbHouse);
        ExcelUtil<TbHouse> util = new ExcelUtil<TbHouse>(TbHouse.class);
        util.exportExcel(response, list, "房屋管理数据");
    }

    /**
     * 获取房屋管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('house:house:query')")
    @GetMapping(value = "/{houseId}")
    public AjaxResult getInfo(@PathVariable("houseId") Long houseId)
    {
        return success(tbHouseService.selectTbHouseByHouseId(houseId));
    }

    /**
     * 新增房屋管理
     */
    @PreAuthorize("@ss.hasPermi('house:house:add')")
    @Log(title = "房屋管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbHouse tbHouse)
    {
        return toAjax(tbHouseService.insertTbHouse(tbHouse));
    }

    /**
     * 修改房屋管理
     */
    @PreAuthorize("@ss.hasPermi('house:house:edit')")
    @Log(title = "房屋管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbHouse tbHouse)
    {
        return toAjax(tbHouseService.updateTbHouse(tbHouse));
    }

    /**
     * 删除房屋管理
     */
    @PreAuthorize("@ss.hasPermi('house:house:remove')")
    @Log(title = "房屋管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{houseIds}")
    public AjaxResult remove(@PathVariable Long[] houseIds)
    {
        return toAjax(tbHouseService.deleteTbHouseByHouseIds(houseIds));
    }

    @GetMapping("/ownerOptions")
    public AjaxResult getOwnerOptions()
    {
        List<TbOwner> list = tbOwnerService.selectTbOwnerList(new TbOwner());
        return AjaxResult.success(list);
    }

    @GetMapping("/owners")
    public AjaxResult getOwnerList()
    {
        List<Map<String, Object>> ownerList = tbOwnerService.selectOwnerListForSelect();
        return AjaxResult.success(ownerList);
    }

    /**
     * 搜索业主
     */
    @GetMapping("/searchOwners")
    public AjaxResult searchOwners(@RequestParam(required = false) String query)
    {
        List<Map<String, Object>> ownerList = tbOwnerService.searchOwnersForSelect(query);
        return AjaxResult.success(ownerList);
    }


}
